package com.example.polarity;


import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Color;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ProgressBar;

public class MainActivity extends Activity implements View.OnClickListener, View.OnTouchListener {
	
	private int TotalLevels = 8;
	public int CurLevel = 1;
	
	//previous level button that was selected
	private View PrevButton; 
	public PlayGuiView playGuiView;
	
	private ImageButton start; 
	Button EndGameButton;
	Button StartGamePlay;
	
	//buttons for the level select screen
	Button MainMenu; 
	Button StartLevel; 
	
	Boolean RunGame = true;
	Boolean startPlay = false;
	Button StartPlayButton;
	
	public GameView GameView = null;
	Boolean SetGameObjects = true;
	
	public ViewGroup LevelObjects;
	
	public Joystick joystick;
	
	public boolean FireWeapon = false;
	
	//boost
	public boolean UseBoost = false;
	public boolean BoostClicked = false;
	public ImageButton BoostBtn; 
	ProgressBar BoostBar;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		//MainView game = new MainView(this);
		  
		setContentView(R.layout.activity_main);
		
		start = (ImageButton)findViewById(R.id.main_start_btn);
		start.setOnClickListener(this);
	
	}

	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

	@SuppressLint("NewApi")
	@Override
	public void onClick(View button) {
		
		if(this.start.equals(button))
		{
			setContentView(R.layout.level_select);
			
			this.MainMenu = (Button)findViewById(R.id.MainMenu);
			this.MainMenu.setOnClickListener(this);
			
			this.StartLevel = (Button)findViewById(R.id.Start_Level);
			this.StartLevel.setOnClickListener(this);
			return;
		}
		
		if(this.StartLevel.equals(button))
		{
			
			
			Level level = new Level(this.CurLevel);
			
			setContentView(level.GetLayoutId());
			LevelObjects = (ViewGroup)this.findViewById(level.GetLevelId());
			StartPlayButton = (Button)findViewById(R.id.StartPlay);
			
			for(int i = 0; i < LevelObjects.getChildCount(); i++)
			{
				if(LevelObjects.getChildAt(i).getTag() != null)
				{
					if(LevelObjects.getChildAt(i).getTag().equals("GameView"))
						this.GameView = (GameView)LevelObjects.getChildAt(i);
					
					if(LevelObjects.getChildAt(i).getTag().equals("Joystick"))
						joystick = (Joystick) LevelObjects.getChildAt(i);
					
					if(LevelObjects.getChildAt(i).getTag().equals("BoostBar"))
						BoostBar = (ProgressBar)LevelObjects.getChildAt(i);
					
				}

				
			}
			
			BoostBtn = (ImageButton)this.findViewById(R.id.BoostBtn);
			BoostBtn.setOnTouchListener(this);
			
			return;
		}
		
		if( (this.MainMenu != null && this.MainMenu.equals(button)))
		{
			setContentView(R.layout.activity_main);
			start = (ImageButton)findViewById(R.id.main_start_btn);
			start.setOnClickListener(this);
			RunGame = false;
			SetGameObjects = true;
			return;
		}
	}
	
	public boolean getSomething()
	{
		
		return false;
	}
	
	//onclick set in the xml file for the level buttons
	public void SetLevel(View button)
	{
		
		if(this.PrevButton != null)
			this.PrevButton.setBackgroundColor(Color.TRANSPARENT);
		
		for(int i = 1; i <= this.TotalLevels; i ++)
		{
			String Id = "Level" + i;
			String ButtonTag = button.getTag().toString();
			//button.setBackgroundColor(Color.TRANSPARENT);
			
			if(ButtonTag.equals(Id))
			{
				CurLevel = i;
				button.setBackgroundColor(Color.GREEN);
				
				this.PrevButton = button;
			}
			
		}
	}
	
	public void PlayButtons(View button)
	{
		String ButtonTag = button.getTag().toString();
		
		if(ButtonTag.equals("Home"))
		{
			setContentView(R.layout.activity_main);
			start = (ImageButton)findViewById(R.id.main_start_btn);
			start.setOnClickListener(this);
		}
		
		if(ButtonTag.equals("Start") )
		{
			startPlay = true;
			
			StartPlayButton.setText("Reset");
			StartPlayButton.setTag("Reset");
			return;
		}
		
		if(ButtonTag.equals("Reset") )
		{
			Button StartPlayButton = (Button)findViewById(R.id.StartPlay);
			StartPlayButton.setText("Start");
			StartPlayButton.setTag("Start");
			return;
		}
	}
	
	public void FireWeapon(View button)
	{
		FireWeapon = true;
	}
	
	public void Boost(View button)
	{
		BoostClicked = true;
	}


	@Override
	public boolean onTouch(View v, MotionEvent event) {
		if(BoostClicked && event.getAction() == MotionEvent.ACTION_DOWN)
			UseBoost = true;
		else
			UseBoost = false;
		
		return false;
	}
}
